
package com.shop.cloud.upms.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.cloud.common.core.util.R;
import com.shop.cloud.common.log.annotation.SysLog;
import com.shop.cloud.upms.admin.service.SysTenantPackageRechargeService;
import com.shop.cloud.upms.common.entity.SysTenantPackageCost;
import com.shop.cloud.upms.admin.service.SysTenantPackageCostService;
import com.shop.cloud.upms.common.entity.SysTenantPackageRecharge;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

/**
 * 租户套餐费用
 *
 * @author 
 * @date 2023-12-25 14:37:23
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/tenantpackagecost")
@Tag(description = "tenantpackagecost", name = "租户套餐费用管理")
public class SysTenantPackageCostController {

    private final SysTenantPackageCostService sysTenantPackageCostService;
    private final SysTenantPackageRechargeService sysTenantPackageRechargeService;

    /**
     * 租户套餐费用list
     * @param sysTenantPackageCost
     * @return
     */
    @Operation(summary = "租户套餐费用list")
    @GetMapping("/list")
    public R getList(SysTenantPackageCost sysTenantPackageCost) {
        return R.ok(sysTenantPackageCostService.list(Wrappers.query(sysTenantPackageCost)));
    }

    /**
     * 租户套餐费用分页列表
     * @param page 分页对象
     * @param sysTenantPackageCost 租户套餐费用
     * @return
     */
    @Operation(summary = "租户套餐费用分页列表")
    @GetMapping("/page")
//    @PreAuthorize("@ato.hasAuthority('upms:tenantpackagecost:index')")
    public R getPage(Page page, SysTenantPackageCost sysTenantPackageCost) {
        return R.ok(sysTenantPackageCostService.page(page, Wrappers.query(sysTenantPackageCost)));
    }

    /**
     * 租户套餐费用查询
     * @param id
     * @return R
     */
    @Operation(summary = "租户套餐费用查询")
    @GetMapping("/{id}")
    @PreAuthorize("@ato.hasAuthority('upms:tenantpackagecost:get')")
    public R getById(@PathVariable("id") String id) {
        return R.ok(sysTenantPackageCostService.getById(id));
    }

    /**
     * 租户套餐费用新增
     * @param sysTenantPackageCost 租户套餐费用
     * @return R
     */
    @Operation(summary = "租户套餐费用新增")
    @SysLog("新增租户套餐费用")
    @PostMapping
    @PreAuthorize("@ato.hasAuthority('upms:tenantpackagecost:add')")
    public R save(@RequestBody SysTenantPackageCost sysTenantPackageCost) {
        return R.ok(sysTenantPackageCostService.save(sysTenantPackageCost));
    }

    /**
     * 租户套餐费用修改
     * @param sysTenantPackageCost 租户套餐费用
     * @return R
     */
    @Operation(summary = "租户套餐费用修改")
    @SysLog("修改租户套餐费用")
    @PutMapping
    @PreAuthorize("@ato.hasAuthority('upms:tenantpackagecost:edit')")
    public R updateById(@RequestBody SysTenantPackageCost sysTenantPackageCost) {
        sysTenantPackageCost.setType(null);
        return R.ok(sysTenantPackageCostService.updateById(sysTenantPackageCost));
    }

    /**
     * 租户套餐费用删除
     * @param id
     * @return R
     */
    @Operation(summary = "租户套餐费用删除")
    @SysLog("删除租户套餐费用")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ato.hasAuthority('upms:tenantpackagecost:del')")
    public R removeById(@PathVariable String id) {
        long count =  sysTenantPackageRechargeService.count(Wrappers.<SysTenantPackageRecharge>lambdaQuery()
                .eq(SysTenantPackageRecharge::getPackageCostId,id));
        if(count > 0){
            return R.failed("该套餐费用已有租户购买，不能删除");
        }
        return R.ok(sysTenantPackageCostService.removeById(id));
    }

}
